<!-- 课程目录列表 -->
<template>
  <div>
    <div class="chapter-header">
      <p class="catalogue-title">目录</p>
      <div @click="addChapterHandle">
        <el-tooltip content="添加章节">
          <span class="el-icon-circle-plus-outline"></span>
        </el-tooltip>
      </div>
    </div>

    <el-collapse v-model="activeName" accordion>
      <div v-for="(item, idx) of chapterIdList" :key="item.id" @click="getPeriodByChapterId(item)">
        <el-collapse-item :name="item.id">
          <template slot="title">
            <div style="display: flex;justify-content:space-between;width: 100%;">
              <div style="display: flex;justify-content: center;align-items: center;">
                <div class="icon">
                  <span>{{ idx + 1 }}</span>
                </div>
                <h3>{{ item.chapterName }}</h3>
              </div>
              <div class="mr-2">
                <span class="mr-2" @click.stop="addPeriodHandle(item)">添加</span>
                <span class="mr-2" @click.stop="changChapter(item)">修改</span>
                <span @click.stop="deleteChapter(item.id)">删除</span>
              </div>
            </div>

          </template>
          <div
            v-for="(itemTitle, index) of periodIdList"
            :key="index"
            class="period-list"
            @click.stop="clickPeriodHandle(item, itemTitle)"
          >
            <span class="period-title">{{ itemTitle.periodName }}</span>
            <div class="period-operation">
              <span @click.stop="changePeriod(item, itemTitle)">修改</span>
              <span @click.stop="deletePeriodById(itemTitle.id)">删除</span>
            </div>
          </div>
        </el-collapse-item>
      </div>
    </el-collapse>
    <chapter-update-and-add-dialog
      v-if="isShowChapterDialog"
      :visible="isShowChapterDialog"
      :form-config="chapterFormConfig"
      :title="chapterDialogTitle"
      @close="isShowChapterDialog = false"
      @handleConfirm="addChapter"
    />
    <chapter-update-and-add-dialog
      v-if="isShowPeriodDialog"
      :visible="isShowPeriodDialog"
      :title="periodDialogTitle"
      :form-config="periodFormConfig"
      @close="isShowPeriodDialog = false"
      @handleConfirm="updateChapterPeriodById"
    />
  </div>
</template>

<script>
import { HttpManager } from "../../../../../request/Teacher";
import ChapterUpdateAndAddDialog from "../chapterUpdateAndAddDialog";

export default {
  name: 'CourseCatalogueList',
  components: { ChapterUpdateAndAddDialog },
  data() {
    return {
      activeName: 0,
      chapterIdList: [],
      periodIdList: [],
      isShowChapterDialog: false,
      isShowPeriodDialog: false,
      chapterFormConfig: {
        chapterName: {
          value: '',
          placeholder: '输入章节名',
          hidden: false
        },
        periodNum: {
          value: '',
          placeholder: '输入课时数量',
          hidden: false
        }
      },
      chapterDialogTitle: '创建章节',
      periodDialogTitle: '创建课时',
      selectChapter: {},
      periodFormConfig: {
        id: {
          value: '',
          hidden: true
        },
        periodName: {
          value: ''
        }
      }
    }
  },
  created() {
    this.getChapterByCourseId()
  },
  methods: {
    addChapterHandle() {
      Object.keys(this.chapterFormConfig).forEach(key => {
        this.chapterFormConfig[key].value = ''
        this.chapterFormConfig[key].hidden = false
      })
      this.chapterDialogTitle = '创建章节'
      this.isShowChapterDialog = true
    },
    addPeriodHandle(chapter) {
      this.selectChapter = chapter
      Object.keys(this.periodFormConfig).forEach(key => {
        this.periodFormConfig[key].value = ''
      })
      this.periodDialogTitle = '创建课时'
      this.isShowPeriodDialog = true
    },
    addChapter(data) {
      if (this.chapterDialogTitle === '创建章节') {
        HttpManager.creatChapterForCourse({
          chapterName: data.chapterName,
          chapterNumber: 1,
          courseId: window.localStorage.getItem("ClassId")
        }, data.periodNum).then(() => {
          this.$message({
            message: '章节创建成功',
            type: 'success'
          });
          this.isShowChapterDialog = false;
          this.getChapterByCourseId();
        })
      } else if (this.chapterDialogTitle === '修改章节') {
        const params = {
          chapterName: data.chapterName,
          chapterNumber: this.selectChapter.chapterNumber,
          courseId: window.localStorage.getItem("ClassId"),
          id: this.selectChapter.id
        }
        HttpManager.updateChapterInf(params).then(() => {
          this.$message({
            message: '章节修改成功',
            type: 'success'
          });
          this.isShowChapterDialog = false;
          this.getChapterByCourseId();
        })
      }
    },
    //获取课程下面的章节列表
    getChapterByCourseId() {
      const classID = window.localStorage.getItem("ClassId");
      HttpManager.getChapterByCourseId({
        courseId: classID,
        isTeacher: true
      }).then(e => {
          this.chapterIdList = e.data.data;
        }
      )
    },
    //获取章节下的课时
    getPeriodByChapterId(data) {
      this.selectChapter = data
      HttpManager.getPeriodByChapterId({
        chapterId: data.id,
        isTeacher: true
      }).then(e => {
          console.log(e);
          this.periodIdList = e.data.data;
        }
      )
    },
    //修改章节的弹框
    changChapter(chapter) {
      this.chapterDialogTitle = '修改章节';
      this.chapterFormConfig.chapterName.value = chapter.chapterName;
      this.selectChapter = chapter
      this.chapterFormConfig.periodNum.hidden = true
      this.isShowChapterDialog = true;
    },
    //删除章节
    deleteChapter(id) {
      HttpManager.deleteChapter(id).then(() => {
        this.$message({
          message: '章节删除成功',
          type: 'success'
        });
        this.getChapterByCourseId()
      })
    },
    //修改课时的弹框
    changePeriod(chapter, period) {
      this.periodFormConfig.id.value = period.id;
      this.periodFormConfig.periodName.value = period.periodName;
      this.periodDialogTitle = '修改课时'
      this.selectChapter = chapter
      this.isShowPeriodDialog = true
    },
    //删除课时
    deletePeriodById(periodId) {
      HttpManager.deleteChapter(periodId).then(e => {
        this.$message({
          message: '课时删除成功',
          type: 'success'
        });
        console.log(e);
      })
    },
    //修改课时
    updateChapterPeriodById(data) {
      if (this.periodDialogTitle === '创建课时') {
        HttpManager.creatPeriod({
          ...data,
          chapterId: this.selectChapter.id,
          periodNumber: 1
        }).then(() => {
          this.$message({
            message: '课时创建成功',
            type: 'success'
          });
          this.isShowPeriodDialog = false;
          this.getPeriodByChapterId(this.selectChapter)
        })
      } else if (this.periodDialogTitle === '修改课时') {
        HttpManager.updatePeriodById({
          ...data,
          chapterId: this.selectChapter.id
        }).then(() => {
          this.$message({
            message: '课时修改成功',
            type: 'success'
          });
          this.isShowPeriodDialog = false;
          this.getPeriodByChapterId(this.selectChapter)
        })
      }
    },
    clickPeriodHandle(chapter, period) {
      this.$emit('clickPeriodHandle', chapter, period)
    }
  }
}
</script>

<style scoped lang="scss">

.mr-2 {
  margin-right: 0.625rem;
}

.chapter-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 10px;
  height: 40px;
  color: #d0d5db;

  .el-icon-circle-plus-outline {
    cursor: pointer;
    &:hover {
      color: #1A8CFE;
    }
  }
}


.el-collapse {
  .icon {
    height: 23px;
    width: 45px;
    line-height: 20px;
    border-radius: 30px;
    background: #d0d5db;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin-left: 15px;

    & > span {
      display: block;
      border-radius: 50%;
      height: 20px;
      width: 20px;
      border: 4px solid #d0d5db;
      background: white;
      text-align: center;
      line-height: 20px;
    }
  }

  .period-list {
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;
    padding: 8px 10px 8px 60px;

    &:hover {
      background: #f0f6ff;
      color: #000000;

      .period-operation {
        display: block;

        span:hover {
          color: #ff0000;
        }
      }
    }

    .period-operation {
      display: none;
    }
  }
}
</style>
